package com.atguigu.system.service.impl;

import com.atguigu.system.mapper.SpuMapper;
import com.atguigu.system.model.entity.Sku;
import com.atguigu.system.model.entity.Spu;
import com.atguigu.system.service.SkuService;
import com.atguigu.system.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-03-01
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuService skuService;

    @Override
    public boolean saveSpu(Spu bizSpu) {
        return save(bizSpu);
    }

    @Override
    public boolean removeSpuById(Integer id) {
        return removeById(id);
    }

    @Override
    public boolean updateSpu(Spu bizSpu) {
        return updateById(bizSpu);
    }

    @Override
    public Spu getSpuById(Integer id) {
        return getById(id);
    }

    @Override
    public List<Spu> listAllSpus() {
        return list();
    }


    @Override
    public Page<Spu> pageSpusByName(Page<Spu> page,String spuName) {
        LambdaQueryWrapper<Spu> queryWrapper = new LambdaQueryWrapper<>();
        if (spuName != null && !spuName.isEmpty()) {
            // 假设 SKU 名称存储在 spuName 字段，可根据实际情况修改
            queryWrapper.like(Spu::getSpuName, spuName);
        }
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }

    @Override
    public Spu selectByID(Integer objectId) {

        Spu spu = spuMapper.selectByID(objectId);
        return spu;
    }


    @Override
    public List<Map<String, Object>> getCascaderOptions() {
        List<Map<String, Object>> options = new ArrayList<>();

        LambdaQueryWrapper<Spu> spuQueryWrapper = new LambdaQueryWrapper<>();
        spuQueryWrapper.eq(Spu::getIsDeleted, 0);
        List<Spu> spus = spuMapper.selectList(spuQueryWrapper);

        for (Spu spu : spus) {
            Map<String, Object> spuOption = new HashMap<>();
            spuOption.put("value", spu.getId());
            spuOption.put("label", spu.getSpuName() + "-" + spu.getBrand());

            LambdaQueryWrapper<Sku> skuQueryWrapper = new LambdaQueryWrapper<>();
            skuQueryWrapper.eq(Sku::getSpuId, spu.getId()).eq(Sku::getIsDeleted, 0);
            List<Sku> skus = skuService.getBaseMapper().selectList(skuQueryWrapper);

            List<Map<String, Object>> skuOptions = new ArrayList<>();
            for (Sku sku : skus) {
                Map<String, Object> skuOption = new HashMap<>();
                skuOption.put("value", sku.getId());
                skuOption.put("label", spu.getSpuName() + "-" + spu.getBrand() + "-" + sku.getColor() + "-" + sku.getSize() + "-" + sku.getQuality());
                skuOptions.add(skuOption);
            }

            spuOption.put("children", skuOptions);
            options.add(spuOption);
        }

        return options;
    }
}
